package com.zcoj.controller.Admin.discussion;

import com.zcoj.core.model.ApiResponse;
import com.zcoj.core.model.PageWrap;
import com.zcoj.pojo.Dto.Discussion;
import com.zcoj.pojo.Dto.DiscussionCategory;
import com.zcoj.pojo.Dto.DiscussionReport;
import com.zcoj.service.DiscussionCategoryService;
import com.zcoj.service.DiscussionReportService;
import com.zcoj.service.DiscussionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author chenbin
 * @version 1.0
 * @description: TODO
 * @date 2023/4/19 11:12
 */
@RequestMapping("/adminDiscussion")
@RestController
@PreAuthorize("hasAnyAuthority('super_admin','admin','discussion_admin')")
public class adminDiscussionController {


    @Autowired
    private DiscussionService discussionService;

    @Autowired
    private DiscussionReportService discussionReportService;

    @Autowired
    private DiscussionCategoryService discussionCategoryService;

    @PostMapping("/discussionList")
    public ApiResponse DiscussionList(@RequestBody PageWrap<Discussion> pageWrap){
        return ApiResponse.success(discussionService.findPage(pageWrap));
    }


    @GetMapping("/delDiscussion/{id}")
    public ApiResponse delDiscussion(@PathVariable Integer id){
        Integer integer = discussionService.deleteById(id);
        if(integer!=0){
            return ApiResponse.success("删除成功",null);
        }else {
            return ApiResponse.failed("删除失败！！！");
        }
    }


    @PostMapping("/updateDiscussion")
    public ApiResponse updateDiscussion(@RequestBody Discussion discussion){
        Integer updateById = discussionService.updateById(discussion);
        if(updateById!=0){
            return ApiResponse.success("更新成功",null);
        }else {
            return ApiResponse.failed("更新失败！！！");
        }
    }

    @GetMapping("/getDiscussionById/{id}")
    public ApiResponse getContestById(@PathVariable Integer id){
        return ApiResponse.success( discussionService.findById(id));
    }

    @GetMapping("/getDiscussionReport/{status}")
    public ApiResponse getDiscussionReport(@PathVariable Integer status){
        DiscussionReport discussionReport = new DiscussionReport();
        discussionReport.setStatus(status);
        List<DiscussionReport> list = discussionReportService.findList(discussionReport);
        return ApiResponse.success(list);
    }


    @GetMapping("/forbiddenDiscussion/{id}")
    public ApiResponse forbiddenDiscussion(@PathVariable Integer id){
        DiscussionReport discussionReport = discussionReportService.findById(id);
        Discussion discussion = discussionService.findById(discussionReport.getDid());
        Integer status = null;
        if(discussion.getStatus()!=0){
            discussion.setStatus(0);
            status=discussionService.updateById(discussion);
        }
        if( Objects.nonNull(status)&&status!=0){
            return ApiResponse.success("禁用成功",null);
        }else {
            return ApiResponse.failed("禁用失败");
        }

    }


    @GetMapping("/getDiscussionCategoryTable")
    public ApiResponse getDiscussionCategoryTable(){
        return ApiResponse.success(discussionCategoryService.findAll());
    }


    @GetMapping("/deleteDiscussionCategoryById/{id}")
    public ApiResponse deleteDiscussionCategoryById(@PathVariable Integer id){
        Integer integer = discussionCategoryService.deleteById(id);
        if(integer!=0){
            return ApiResponse.success("删除成功");
        }else {
            return ApiResponse.failed("删除失败");
        }

    }


    @PostMapping("/createDiscussionCategory")
    public ApiResponse createDiscussionCategory(@RequestBody DiscussionCategory discussionCategory){
        if(Objects.nonNull(discussionCategory.getName())){
            discussionCategoryService.create(discussionCategory);
            if(Objects.nonNull(discussionCategory.getId())){
                return ApiResponse.success("创建成功");
            }else {
                return ApiResponse.failed("创建失败");
            }
        }else {
            return ApiResponse.failed("信息错误");
        }
    }


    @GetMapping("/findDiscussionCategoryById/{id}")
    public ApiResponse findDiscussionCategoryById(@PathVariable Integer id){
        DiscussionCategory byId = discussionCategoryService.findById(id);
        if(Objects.nonNull(byId)){
            return ApiResponse.success(byId);
        }else {
            return ApiResponse.failed("无此DiscussionCategoryId");
        }

    }

    @PostMapping("/updateDiscussionCategory")
    public ApiResponse updateDiscussionCategory(@RequestBody DiscussionCategory discussionCategory){
        Integer integer = discussionCategoryService.updateById(discussionCategory);
        if(integer!=0){
            return ApiResponse.success("更新成功");
        }else {
            return ApiResponse.failed("更新失败");
        }

    }
}
